package com.example.myapplication;

import android.app.WallpaperManager;

import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

public class MainActivity extends AppCompatActivity {
    int[] wallpapers ;
    Button confi;
    ImageView imgs;
    WallpaperManager wallpaperManager;
    int current = 0;
    BitmapDrawable bitmap;
   private void  inti (){
       confi = (Button) findViewById(R.id.confi) ;
       imgs = (ImageView) findViewById(R.id.img) ;
       WallpaperManager.getInstance(MainActivity.this);
       wallpapers = new int[]{R.drawable.img1};
   }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        inti ();
        confi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bitmap = (BitmapDrawable) getResources().getDrawable(wallpapers[current]);
                try {
                    wallpaperManager.setBitmap(bitmap.getBitmap());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                Toast.makeText(MainActivity.this, "壁纸设置成功！", Toast.LENGTH_SHORT).show();
            }

        });


    }
    }
